<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function index() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run('generate_report') == FALSE) {
			$this->_load_common_header();
			$this->_load_common_navigation();
			$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'report'));
			$this->load->view('mahalum/report_form');
			$this->_load_common_footer();
		} else {
			$submit = $this->input->post('submit');
			$id = $this->input->post('id');
			$start = $this->input->post('start');
			$end = $this->input->post('end');
			if ($submit == 'Download Laporan') {
				$this->_generate_report($id, $start, $end);
			} else if ($submit == 'Lihat Laporan') {
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'report'));
				$this->load->view('mahalum/report_form', array('id' => $id));
				$this->_generate_view($id, $start, $end);
				$this->_load_common_footer();
			} else {
				redirect('mahalum/report/index', 'location');
				exit;
			}
		}
	}
	
	private function _generate_report($id, $start, $end) {
		$this->load->library('reports');
		if ($id == '1') {
			$data = $this->reports->generate_recievers_report($start, $end);
			$params = array(
				'data' => $data,
				'start' => $start,
				'end' => $end
			);
			$this->load->view('report/recievers_report', $params);
		} else if ($id == '2') {
			$data = $this->reports->generate_registrants_report($start, $end);
			$params = array(
				'data' => $data,
				'start' => $start,
				'end' => $end
			);
			$this->load->view('report/rejects_report', $params);	
		} else if ($id == '3') {
			$data = $this->reports->generate_current_registrants();
			$params= array(
				'data' => $data
			);
			$this->load->view('report/registrants_report', $params);
		}
	}
	
	private function _generate_view($id, $start, $end) {
		$this->load->library('reports');
		$this->load->helper('table');
		if ($id == '1') {
			$data = $this->reports->generate_recievers_report($start, $end);
			$params = array(
				'data' => $data,
				'start' => $start,
				'end' => $end,
				'pager' => get_pager(7)
			);
			$this->load->view('mahalum/recievers_report', $params);
		} else if ($id == '2') {
			$data = $this->reports->generate_registrants_report($start, $end);
			$params = array(
				'data' => $data,
				'start' => $start,
				'end' => $end,
				'pager' => get_pager(4)
			);
			$this->load->view('mahalum/rejects_report', $params);
		} else if ($id == '3') {
			$data = $this->reports->generate_current_registrants();
			$params= array(
				'data' => $data,
				'pager' => get_pager(6)
			);
			$this->load->view('mahalum/registrants_report', $params);
		}
	}

	public function _check_category($end) {
		$start = $this->input->post('start');
		$id = $this->input->post('id');
		$flag = TRUE;
		if ($id == '1' || $id == '2') {
			if (preg_match('/^[0-9]{4}$/', trim($start)) == 1 &&
				preg_match('/^[0-9]{4}$/', trim($end)) == 1) {
				$start = intval($start);
				$end = intval($end);
				if ($start > $end) {
					$flag = FALSE;
				}
			} else {
				$flag = FALSE;
			}
		}
		if ($flag) {
			return TRUE;
		} else {
			$this->form_validation->set_message('_check_category', 'Tahun periode harus valid, mulai <= akhir.');
			return FALSE;
		}
	}	
}